package com.dg.gaming.server.data.mysql;

import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.sql.*;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2009</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class DBUser extends ADBObject {

    private static final String SQL_LOAD_FULL =
            "select user_id, user_name from user where user_name=?";
    private static final String SQL_LOAD_FULL_WITH_ID =
            "select user_id, user_name from user where user_id=?";
    public static final PrepareStatmentObject PSO_LOAD_FULL = new
            PrepareStatmentObject(SQL_LOAD_FULL);
    public static final PrepareStatmentObject PSO_LOAD_FULL_WITH_ID = new
            PrepareStatmentObject(SQL_LOAD_FULL_WITH_ID);
    private long userId;
    private String username;

    public DBUser(long userId, String username) {
        this.userId = userId;
        this.username = username;
    }

    public DBUser() {

    }


    public long getUserId() {
        return userId;
    }

    public String getUsername() {
        return username;
    }

    public void setUserId(long userId) {
        this.userId = userId;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    protected void loadFull(ResultSet rs) throws Exception {
        setUserId(rs.getLong("user_id"));
        setUsername(rs.getString("user_name"));
    }

    protected boolean load(String userName) throws Exception {
        PreparedStatement ps = PSO_LOAD_FULL.getPrepareStmt();
        ResultSet r = null;
        try {
            ps.setString(1, userName); ;
            r = ps.executeQuery();
            if (r.next()) {
                loadSimple(r);
                return true;
            } else {
                return false;
            }

        } finally {
            if (r != null) {
                r.close();
            }

        }

    }

    protected boolean load(long userId) throws Exception {
        PreparedStatement ps = PSO_LOAD_FULL_WITH_ID.getPrepareStmt();
        ResultSet r = null;
        try {
            ps.setLong(1, userId); ;
            r = ps.executeQuery();
            if (r.next()) {
                loadSimple(r);
                return true;
            } else {
                return false;
            }

        } finally {
            if (r != null) {
                r.close();
            }

        }

    }

    protected void save(PreparedStatement ps) throws Exception {
    }

    protected void loadSimple(ResultSet rs) throws Exception {
        loadFull(rs);
    }

}
